﻿using ODS.Config;
using ODS.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ODS.ViewModel
{
    public class MessageViewModel : INotifyPropertyChanged
    {
        public MessageViewModel(EventDetailModel _eventDetailModel)
        {
            EventDetailModel = _eventDetailModel;

            NotifyPropertyChanged("DetailLocation");
            NotifyPropertyChanged("FormatedDateTime");
            NotifyPropertyChanged("EventType");
        }

        EventDetailModel eventDetailModel;

        public EventDetailModel EventDetailModel
        {
            get { return eventDetailModel; }
            set { eventDetailModel = value; }
        }


        String detailLocation;

        public String DetailLocation
        {
            get {
                switch (int.Parse(eventDetailModel.EventType))
                {
                    case Constants.MONITOR_TYPE_HARSH_BRAKING:
                         detailLocation =  handleMONITOR_TYPE_HARSH_BRAKING(); break;
                    case Constants.MONITOR_TYPE_HARSH_CORNERING:
                        detailLocation = handleMONITOR_TYPE_HARSH_CORNERING(); break;
                    case Constants.MONITOR_TYPE_IMPACT:
                        detailLocation = hanldeMONITOR_TYPE_IMPACT(); break;
                    case Constants.MONITOR_TYPE_IDLE:
                        detailLocation = handleMONITOR_TYPE_IDLE(); break;
                    case Constants.MONITOR_TYPE_GEOFENCE:
                        detailLocation = handleMONITOR_TYPE_GEOFENCE(); break;
                    case Constants.MONITOR_TYPE_POWER:
                        detailLocation = hanldeMONITOR_TYPE_POWER(); break;
                    case Constants.MONITOR_TYPE_SPEED:
                        detailLocation = handleMONITOR_TYPE_SPEED(); break;
                    case Constants.MONITOR_TYPE_TOWING:
                        detailLocation = hanldeMONITOR_TYPE_TOWING(); break;
                    case Constants.EVENT_POSITION:
                        detailLocation = handleEVENT_POSITION(); break;
                    case Constants.REQUEST_COMMAND:
                        detailLocation = hanldeREQUEST_COMMAND(); break;
                    case Constants.EVENT_MONITOR_PROCESS:
                        detailLocation = handleEVENT_MONITOR_PROCESS(); break;

                }
    
                return detailLocation;
            
            }
            set {
                detailLocation = value;
                NotifyPropertyChanged("DetailLocation");
            }
        }

        #region handle monitor precess
        private string handleEVENT_MONITOR_PROCESS()
        {
            String detailLocation = "";
            if (!String.IsNullOrEmpty(eventDetailModel.MonitorType))
            {
                switch (int.Parse(eventDetailModel.MonitorType))
                {
                    case Constants.MONITOR_TYPE_HARSH_BRAKING:
                        detailLocation = "Monitor Harsh Braking processed"; break;
                    case Constants.MONITOR_TYPE_HARSH_CORNERING:
                        detailLocation = "Monitor Harsh Cornering processed"; break;
                    case Constants.MONITOR_TYPE_IMPACT:
                        detailLocation = "Monitor Impact processed"; break;
                    case Constants.MONITOR_TYPE_IDLE:
                        detailLocation = "Monitor Idling processed"; break;
                    case Constants.MONITOR_TYPE_GEOFENCE:
                        detailLocation = "Monitor Geofence processed"; break;
                    case Constants.MONITOR_TYPE_POWER:
                        detailLocation = "Monitor Power processed"; break;
                    case Constants.MONITOR_TYPE_SPEED:
                        detailLocation = "Monitor  Speeding processed"; break;
                    case Constants.MONITOR_TYPE_TOWING:
                        detailLocation = "Monitor Towing processed"; break;
                }
            }
            return detailLocation;
        }

        private string hanldeREQUEST_COMMAND()
        {
            String detailLocation = "";
            if (!String.IsNullOrEmpty(eventDetailModel.CommandType))
            {
                switch (int.Parse(eventDetailModel.CommandType))
                {
                    case Constants.REQUEST_POSITION: detailLocation = "Request for location"; break;
                    case Constants.BLAST_TRACKING: detailLocation = "Blast tracking"; break;
                    case Constants.KILL_STARTER: detailLocation = "Kill starter"; break;
                    case Constants.CANCEL_STATER: detailLocation = "Cancel starter kill"; break;
                    case Constants.REBOOT_DEVICE: detailLocation = "Reboot device"; break;
                    case Constants.SET_ODOMETOR: detailLocation = "Set odometer"; break;
                }
            }
            
            return detailLocation;
        }

        private string handleEVENT_POSITION()
        {
            String detailLocation = "Vehicle is located at...";
            if (eventDetailModel.MessageModel != null)
            {
                if (String.IsNullOrEmpty(eventDetailModel.MessageModel.Address))
                {
                    if (eventDetailModel.MessageModel.Latitude.Length > 0 && eventDetailModel.MessageModel.Longitude.Length > 0)
                    {
                        GoogleServiceHandler googleService = new GoogleServiceHandler();
                        googleService.OnGetGeocodingLocation += googleService_OnGetGeocodingLocation;
                        googleService.GetPlaceByLatLng(double.Parse(eventDetailModel.MessageModel.Latitude), double.Parse(eventDetailModel.MessageModel.Longitude));
                    }
                    else
                    {
                        detailLocation = @"Vehicle is located at...";
                    }
                }
                else
                {
                    detailLocation = "Vehicle is located at " + eventDetailModel.MessageModel.Address;
                }
            }
            else
            {
                detailLocation = @"Vehicle is located at...";
            }

            return detailLocation;
        }

        private string hanldeMONITOR_TYPE_TOWING()
        {
            String detailLocation = "Towing";
            if (eventDetailModel.MessageModel != null)
            {
                if (String.IsNullOrEmpty(eventDetailModel.MessageModel.Address))
                {
                    if (eventDetailModel.MessageModel.Latitude.Length > 0 && eventDetailModel.MessageModel.Longitude.Length > 0)
                    {
                        GoogleServiceHandler googleService = new GoogleServiceHandler();
                        googleService.OnGetGeocodingLocation += googleService_OnGetGeocodingLocation;
                        googleService.GetPlaceByLatLng(double.Parse(eventDetailModel.MessageModel.Latitude), double.Parse(eventDetailModel.MessageModel.Longitude));
                    }
                    else
                    {
                        detailLocation = @"Towing";
                    }
                }
                else
                {
                    detailLocation = "Towing at " + eventDetailModel.MessageModel.Address;
                }
            }
            else
            {
                detailLocation = @"Towing";
            }
            return detailLocation;
        }

        private string handleMONITOR_TYPE_SPEED()
        {
            String detailLocation = "Speeding";
            if (eventDetailModel.MessageModel != null)
            {
                if (String.IsNullOrEmpty(eventDetailModel.MessageModel.Address))
                {
                    if (eventDetailModel.MessageModel.Latitude.Length > 0 && eventDetailModel.MessageModel.Longitude.Length > 0)
                    {
                        GoogleServiceHandler googleService = new GoogleServiceHandler();
                        googleService.OnGetGeocodingLocation += googleService_OnGetGeocodingLocation;
                        googleService.GetPlaceByLatLng(double.Parse(eventDetailModel.MessageModel.Latitude), double.Parse(eventDetailModel.MessageModel.Longitude));
                    }
                    else
                    {
                        detailLocation = @"Speeding";
                    }
                }
                else
                {
                    detailLocation = "Speeding at " + eventDetailModel.MessageModel.Address;
                }
            }
            else
            {
                detailLocation = @"Speeding";
            }
            return detailLocation;
        }

        private string hanldeMONITOR_TYPE_POWER()
        {
            String detailLocation = "Power low";
            if (eventDetailModel.MessageModel != null)
            {
                if (String.IsNullOrEmpty(eventDetailModel.MessageModel.Address))
                {
                    if (eventDetailModel.MessageModel.Latitude.Length > 0 && eventDetailModel.MessageModel.Longitude.Length > 0)
                    {
                        GoogleServiceHandler googleService = new GoogleServiceHandler();
                        googleService.OnGetGeocodingLocation += googleService_OnGetGeocodingLocation;
                        googleService.GetPlaceByLatLng(double.Parse(eventDetailModel.MessageModel.Latitude), double.Parse(eventDetailModel.MessageModel.Longitude));
                    }
                    else
                    {
                        detailLocation = @"Power low";
                    }
                }
                else
                {
                    detailLocation = "Power low at " + eventDetailModel.MessageModel.Address;
                }
            }
            else
            {
                detailLocation = @"Power low";
            }
            return detailLocation;
        }

        private string handleMONITOR_TYPE_GEOFENCE()
        {
            String detailLocation = "GeoFence";
            if (eventDetailModel.MessageModel != null)
            {
                if (String.IsNullOrEmpty(eventDetailModel.MessageModel.Address))
                {
                    if (eventDetailModel.MessageModel.Latitude.Length > 0 && eventDetailModel.MessageModel.Longitude.Length > 0)
                    {
                        GoogleServiceHandler googleService = new GoogleServiceHandler();
                        googleService.OnGetGeocodingLocation += googleService_OnGetGeocodingLocation;
                        googleService.GetPlaceByLatLng(double.Parse(eventDetailModel.MessageModel.Latitude), double.Parse(eventDetailModel.MessageModel.Longitude));
                    }
                    else
                    {
                        detailLocation = @"GeoFence";
                    }
                }
                else
                {
                    detailLocation = "GeoFence at " + eventDetailModel.MessageModel.Address;
                }
            }
            else
            {
                detailLocation = @"GeoFence";
            }
            return detailLocation;
        }

        private string handleMONITOR_TYPE_IDLE()
        {
            String detailLocation = "Idling";
            if (eventDetailModel.MessageModel != null)
            {
                if (String.IsNullOrEmpty(eventDetailModel.MessageModel.Address))
                {
                    if (eventDetailModel.MessageModel.Latitude.Length > 0 && eventDetailModel.MessageModel.Longitude.Length > 0)
                    {
                        GoogleServiceHandler googleService = new GoogleServiceHandler();
                        googleService.OnGetGeocodingLocation += googleService_OnGetGeocodingLocation;
                        googleService.GetPlaceByLatLng(double.Parse(eventDetailModel.MessageModel.Latitude), double.Parse(eventDetailModel.MessageModel.Longitude));
                    }
                    else
                    {
                        detailLocation = @"Idling";
                    }
                }
                else
                {
                    detailLocation = "Idling at " + eventDetailModel.MessageModel.Address;
                }
            }
            else
            {
                detailLocation = @"Idling";
            }
            return detailLocation;
        }

        private string hanldeMONITOR_TYPE_IMPACT()
        {
            String detailLocation = "Impact";
            if (eventDetailModel.MessageModel != null)
            {
                if (String.IsNullOrEmpty(eventDetailModel.MessageModel.Address))
                {
                    if (eventDetailModel.MessageModel.Latitude.Length > 0 && eventDetailModel.MessageModel.Longitude.Length > 0)
                    {
                        GoogleServiceHandler googleService = new GoogleServiceHandler();
                        googleService.OnGetGeocodingLocation += googleService_OnGetGeocodingLocation;
                        googleService.GetPlaceByLatLng(double.Parse(eventDetailModel.MessageModel.Latitude), double.Parse(eventDetailModel.MessageModel.Longitude));
                    }
                    else
                    {
                        detailLocation = @"Impact";
                    }
                }
                else
                {
                    detailLocation = "Impact at " + eventDetailModel.MessageModel.Address;
                }
            }
            else
            {
                detailLocation = @"Impact";
            }

            return detailLocation;
        }

        private string handleMONITOR_TYPE_HARSH_CORNERING()
        {
            String detailLocation = "Harsh Cornering";
            if (eventDetailModel.MessageModel != null)
            {
                if (String.IsNullOrEmpty(eventDetailModel.MessageModel.Address))
                {
                    if (eventDetailModel.MessageModel.Latitude.Length > 0 && eventDetailModel.MessageModel.Longitude.Length > 0)
                    {
                        GoogleServiceHandler googleService = new GoogleServiceHandler();
                        googleService.OnGetGeocodingLocation += googleService_OnGetGeocodingLocation;
                        googleService.GetPlaceByLatLng(double.Parse(eventDetailModel.MessageModel.Latitude), double.Parse(eventDetailModel.MessageModel.Longitude));
                    }
                    else
                    {
                        detailLocation = @"Harsh Cornering";
                    }
                }
                else
                {
                    detailLocation = "Harsh Cornering at " + eventDetailModel.MessageModel.Address;
                }
            }
            else
            {
                detailLocation = @"Harsh Cornering";
            }
            return detailLocation;
        }

        private string handleMONITOR_TYPE_HARSH_BRAKING()
        {
            String detailLocation = "Harsh Braking";
            if (eventDetailModel.MessageModel != null)
            {
                if (String.IsNullOrEmpty(eventDetailModel.MessageModel.Address))
                {
                    if (eventDetailModel.MessageModel.Latitude.Length > 0 && eventDetailModel.MessageModel.Longitude.Length > 0)
                    {
                        GoogleServiceHandler googleService = new GoogleServiceHandler();
                        googleService.OnGetGeocodingLocation += googleService_OnGetGeocodingLocation;
                        googleService.GetPlaceByLatLng(double.Parse(eventDetailModel.MessageModel.Latitude), double.Parse(eventDetailModel.MessageModel.Longitude));
                    }
                    else
                    {
                        detailLocation = @"Harsh Braking";
                    }
                }
                else
                {
                    detailLocation = "Harsh Braking at " + eventDetailModel.MessageModel.Address;
                }
            }
            else
            {
                detailLocation = @"Harsh Braking";
            }
            return detailLocation;
        }
        #endregion

        void googleService_OnGetGeocodingLocation(string address, double lat, double lng)
        {
            eventDetailModel.MessageModel.Address = address;
            NotifyPropertyChanged("DetailLocation");
            
        }

        String formatedDateTime;

        public String FormatedDateTime
        {
            get {
                formatedDateTime = eventDetailModel.RegisteredAt.ToString("dd MMM yyyy HH:mm tt");
                return formatedDateTime;
            }
            set {
                formatedDateTime = value;
                NotifyPropertyChanged("FormatedDateTime");
            }
        }

        String eventType;

        public String EventType
        {
            get {
                eventType = "";

                foreach(EventTypeModel _eventType in App.EventTypeModelList)
                {
                    if (_eventType.Code == eventDetailModel.EventType)
                    {
                        eventType = _eventType.Mnemonics;
                        break;
                    }
                }                
                return eventType;
            }
            set {
                eventType = value;
                NotifyPropertyChanged("EventType");
            }
        }

        DateTime readAt;

        public DateTime ReadAt
        {
            get { return readAt; }
            set { 
                readAt = value;
                eventDetailModel.ReadAt = value;
                NotifyPropertyChanged("ForeGroundLocation");
            }
        }

        String foreGroundLocation;

        public String ForeGroundLocation
        {
            get {
                if (eventDetailModel.ReadAt == null || eventDetailModel.ReadAt == DateTime.MinValue)
                {
                    foreGroundLocation = "#FFBC0000";
                }
                else
                {
                    foreGroundLocation = "White";
                }
                return foreGroundLocation;
            }
            set {
                foreGroundLocation = value;
                NotifyPropertyChanged("ForeGroundLocation");
            }
        } 

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
